package chapter14.h14_4;

public abstract class Support {
    private String name;
    private Support next;

    public Support(String name) {
        this.name = name;
    }

    public Support setNext(Support next) {
        this.next = next;
        return next;
    }

    public final void support(Trouble trouble) {
        Support s = this;
        while(s!=null){
            if(s.resolve(trouble)){
                s.done(trouble);
            }else if(s.next != null){
                s = s.next;
            }else {
                s.fail(trouble);
            }
        }

    }

    protected abstract boolean resolve(Trouble trouble);

    protected void done(Trouble trouble) {
        System.out.println(trouble + " is resolved by " + this + ".");
    }

    protected void fail(Trouble trouble) {
        System.out.println(trouble + " cannot be resolved.");
    }
}
